package io.lkt.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.entity.SysUserEntity;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.lkt.modules.sys.entity.BMessageKefuEntity;
import io.lkt.modules.sys.service.BMessageKefuService;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;



/**
 * 微信公众号客服联系方式
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2020-12-21 15:02:45
 */
@RestController
@RequestMapping("/sys/bmessagekefu")
public class BMessageKefuController extends AbstractController {
    @Autowired
    private BMessageKefuService bMessageKefuService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:bmessagekefu:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bMessageKefuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{gzhCode}")
//    @RequiresPermissions("sys:bmessagekefu:info")
    public R info(@PathVariable("gzhCode") String gzhCode){

        List<BMessageKefuEntity> bMessageKefuEntityList = bMessageKefuService.queryPage(gzhCode);

        return R.ok().put("bMessageKefuEntityList", bMessageKefuEntityList);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:bmessagekefu:save")
    public R save(@RequestBody BMessageKefuEntity bMessageKefu){
        bMessageKefuService.save(bMessageKefu);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:bmessagekefu:update")
    public R update(@RequestBody BMessageKefuEntity bMessageKefu){
        ValidatorUtils.validateEntity(bMessageKefu);
        bMessageKefuService.updateById(bMessageKefu);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:bmessagekefu:delete")
    public R delete(@RequestBody Integer[] ids){
        bMessageKefuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 更新
     */
    @RequestMapping("/upSave")
//    @RequiresPermissions("sys:bmessagekefu:save")
    public R save(@RequestBody List<BMessageKefuEntity> list){

        //获取当前登录用户
        SysUserEntity curUser = getUser();

        if (CollectionUtils.isNotEmpty(list)) {
            String gzhCode = list.get(0).getGzhCode();

            List<BMessageKefuEntity> bMessageKefuEntityList = bMessageKefuService.queryPage(gzhCode);

            if (CollectionUtils.isNotEmpty(bMessageKefuEntityList)) {
                for (BMessageKefuEntity info: bMessageKefuEntityList) {
                    info.setStatus(0);
                    info.setUpdateBy(curUser.getUsername());
                    info.setUpdateTime(new Date());
                    bMessageKefuService.updateById(info);
                }
            }

            for (BMessageKefuEntity info: list) {
                info.setStatus(1);
                info.setCreateBy(curUser.getUsername());
                info.setCreateTime(new Date());
                info.setUpdateBy(curUser.getUsername());
                info.setUpdateTime(new Date());
                bMessageKefuService.save(info);
            }

        }

        return R.ok();
    }

}
